package com.example.perplonews.a_d_fragment.a_news


import BaseFragment
import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.blankj.utilcode.util.PhoneUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.perplonews.R
import com.example.perplonews.a_a_mvp.contract.News_Contract
import com.example.perplonews.a_a_mvp.model.HttpCode
import com.example.perplonews.a_a_mvp.model.models.News_Model
import com.example.perplonews.a_a_mvp.presenter.presenters.News_Presenter
import com.example.perplonews.a_b_entity.BaseEntity
import com.example.perplonews.a_b_entity.MoreChannel
import com.example.perplonews.a_b_entity.TextBannerEntity
import com.example.perplonews.a_d_fragment.a_news.adapter.TextBannerAdapter
import com.example.perplonews.a_d_fragment.a_news.adapter.VipAdapter
import com.flyco.tablayout.SlidingTabLayout
import com.youth.banner.Banner
import com.youth.banner.listener.OnBannerListener
import com.youth.banner.transformer.ZoomOutPageTransformer


class NewsFragment : BaseFragment<News_Presenter>(), News_Contract.NewsView,
    OnBannerListener<TextBannerEntity.Values> {
    private val newsIcon: ImageView by lazy { findViewById<ImageView>(R.id.news_icon) }
    private val newsAudioImg: ImageView by lazy { findViewById<ImageView>(R.id.news_audio_img) }
    private val newsSearchIcon: ImageView by lazy { findViewById<ImageView>(R.id.news_search_icon) }
    private val newsMoreImg: ImageView by lazy { findViewById<ImageView>(R.id.news_more_img) }
    private val newsTab: SlidingTabLayout by lazy { findViewById<SlidingTabLayout>(R.id.news_tab) }
    private val newsVp: ViewPager by lazy { findViewById<ViewPager>(R.id.news_vp) }
    private val newsTextBanner: Banner<TextBannerEntity, TextBannerAdapter> by lazy {
        findViewById<Banner<TextBannerEntity, TextBannerAdapter>>(
            R.id.news_text_banner
        )
    }
    private var channel_id: Long? = null

    var bannerAdapter: TextBannerAdapter? = null

    override fun findlayout(): Int {
        return R.layout.fragment_news
    }

    override fun initView() {

    }


    @SuppressLint("MissingPermission")
    override fun initData() {
        mypre = News_Presenter(News_Model(), this)
        //搜索banner
        var map: HashMap<String, Any> = HashMap()
        map.put("code", HttpCode.TEXTBANNERCODE)
        //tab banner
        var map2: HashMap<String, Any> = HashMap()
        map2.put("code", HttpCode.MORECHANNELCODE)


        var list: ArrayList<HashMap<String, Any>> = ArrayList()
        list.add(map)

        var userid =
            activity?.getSharedPreferences("userid", Context.MODE_PRIVATE)?.getString("id", null)
        if (userid != null) {
            map2.put("imie", PhoneUtils.getIMEI())
            map2.put("userId", userid!!.toInt())
            list.add(map2)
            mypre.requestAll(list)
        }


//        mypre.requestAll(list)
    }


    override fun refreshAll(entity: BaseEntity) {
        if (entity is TextBannerEntity) {
            var textbanner = entity as TextBannerEntity
            var values = textbanner.getValues()

            //把数据给banners
            if (values == null) {

            }else{
                bannerAdapter = TextBannerAdapter(values as ArrayList<TextBannerEntity.Values>)
                newsTextBanner.setAdapter(bannerAdapter)
                    .setOrientation(Banner.VERTICAL)
                    .setPageTransformer(ZoomOutPageTransformer())
                    .setOnBannerListener(this)
            }

        } else if (entity is MoreChannel) {

            var list: ArrayList<Fragment> = ArrayList()
            var moreChannel = entity as MoreChannel
            var values = moreChannel.getValues()

            if (values == null) {
                return
            }
//            for (i in 0..values.size - 1) {
//                newsTab.addTab(newsTab.newTab().setText(values[i].channel_name))
//            }


//            加载fragment
//            for (i in 0..values.size-1) {
//                //传id过去
//
//
//            }
            list.add(BlankFragment(values[0].channel_id))
            //请求数据
            var vipAdapter = VipAdapter(activity?.supportFragmentManager!!, list, values)
            newsVp.adapter = vipAdapter
            newsTab.setViewPager(newsVp)

        }

    }

    //banner点击
    override fun OnBannerClick(data: TextBannerEntity.Values, position: Int) {
        ToastUtils.showShort(data.news_title)
    }

    //跳转页面
    override fun startPage(bundle: Bundle, clazz: Class<Any>) {
    }

    override fun onStart() {
        super.onStart()
        newsTextBanner.start()
    }

    override fun onStop() {
        super.onStop()
        newsTextBanner.stop()
    }

    override fun onDestroy() {
        super.onDestroy()
        newsTextBanner.destroy()
    }


}